﻿using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Areas.Mobile.Lib;

namespace Web.Areas.Mobile.Filter
{
    public class IsRegister : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            var requestUrl = filterContext.HttpContext.Request.RawUrl;
            var currentUser = new wx_usersLogic().Getwx_users(MobileAccount.ID) ?? new wx_users();
            bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(NotCheckIsRegister), inherit: true)
                                        || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(NotCheckIsRegister), inherit: true);
            if (!skipAuthorization)
            {
                var url = new UrlHelper(filterContext.RequestContext);
                if (string.IsNullOrEmpty(currentUser.mobile) || currentUser.type == (int)UserType.None)
                {
                    if (filterContext.HttpContext.Request.IsAjaxRequest())
                    {
                        JsonResult jsonResult = new JsonResult()
                        {
                            Data = new
                            {
                                status = false,
                                errmsg = "请先进行身份认证！"
                            }
                        };
                        filterContext.Result = jsonResult;
                    }
                    else
                    {
                        filterContext.Result = new RedirectResult(url.Action("Register", "User", new { area = "Mobile", returnUrl = requestUrl }));
                    }
                }
            }
        }
    }
    public class NotCheckIsRegister : Attribute { }

}